package org.lemon.ddd.web;

import lombok.RequiredArgsConstructor;
import org.lemon.ddd.application.dto.UserRoleDTO;
import org.lemon.ddd.application.service.AuthorizeApplicationService;
import org.lemon.ddd.common.result.BaseResult;
import org.lemon.ddd.common.result.Result;
import org.lemon.ddd.converter.AuthorizeConverter;
import org.lemon.ddd.model.req.AuthorizeCreateReq;
import org.lemon.ddd.model.req.AuthorizeUpdateReq;
import org.lemon.ddd.model.vo.UserAuthorizeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理web接口
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class AuthorizeController {

    private final AuthorizeConverter authorizeConverter;

    private final AuthorizeApplicationService authorizeApplicationService;

    /**
     * 测试URL：<a href="http://127.0.0.1:8087/api/user?userId=xxx">...</a>
     */
    @GetMapping("/query")
    public Result<UserAuthorizeVO> query(@RequestParam("userId") Long userId){
        UserRoleDTO userRoleDTO = authorizeApplicationService.queryUserAuthorize(userId);
        Result<UserAuthorizeVO> result = new Result<>();
        result.setData(authorizeConverter.toVO(userRoleDTO));
        result.setCode(BaseResult.CODE_SUCCESS);
        return result;
    }

    /**
     * 测试URL：<a href="http://127.0.0.1:8087/api/user/delete?userId=xxx">...</a>
     */
    @DeleteMapping("/delete")
    public Result<Object> delete(@RequestParam("userId") Long userId){
        authorizeApplicationService.deleteUserAuthorize(userId);
        return Result.ok(BaseResult.DELETE_SUCCESS);
    }

    /**
     * 测试URL：<a href="http://127.0.0.1:8087/api/user/save">...</a>
     * Post Body：{"userName":"tom","realName":"楼","phone":13123676844,"password":"***","unitId":2,"province":"湖北省","city":"鄂州市","county":"葛店开发区","roles":[{"roleId":2}]}
     */
    @PostMapping("/save")
    public Result<Object> create(@RequestBody AuthorizeCreateReq authorizeCreateReq){
        authorizeApplicationService.createUserAuthorize(authorizeConverter.toDTO(authorizeCreateReq));
        return Result.ok(BaseResult.INSERT_SUCCESS);
    }

    /**
     * 测试URL：<a href="http://127.0.0.1:8087/api/user/update">...</a>
     * Post Body：{"userId":1,"userName":"tom","realName":"Jack","phone":123,"roles":[{"roleId":2}]}
     */
    @PutMapping("/update")
    public Result<Object> update(@RequestBody AuthorizeUpdateReq authorizeUpdateReq){
        authorizeApplicationService.updateUserAuthorize(authorizeConverter.toDTO(authorizeUpdateReq));
        return Result.ok(BaseResult.UPDATE_SUCCESS);
    }
}
